//
//  LYEmoticonToolBar.swift
//  EmoticonKeyboard
//
//  Created by liuerying on 16/5/30.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit

private let baseTag = 10

class LYEmoticonToolBar: UIStackView {

    //定义属性 ,记录选中的按钮
    var curreBtn: UIButton?
    
    //定义闭包--可选项的闭包
    var selectedIndexCallBack: ((index: Int) -> ())?
    
    init(){
        super.init(frame: CGRectZero)
        backgroundColor = UIColor.redColor()
        //设置轴
        axis = .Horizontal
        //设置填充模式
        distribution = .FillEqually
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI界面
    private func setupUI(){
//        addChildButtons("默认", backImageName: "compose_emotion_table_left", index: 0)
//        addChildButtons("Emoji", backImageName: "compose_emotion_table_mid", index: 1)
//        addChildButtons("浪小花", backImageName: "compose_emotion_table_right", index: 2)
        let packages = LYEmoticonManager.sharedEmoticonTools.packages
        
        for i in 0..<packages.count {
        
            let package = packages[i]
            var backImageName = "compose_emotion_table_mid"
            
            if i == 0 {
            
                backImageName = "compose_emotion_table_left"
            }else if i == packages.count - 1{
            
                backImageName = "compose_emotion_table_right"
            }
            
            addChildButtons(package.title!, backImageName: backImageName, index: i)
        }
        
        
    }
    
    //封装按钮添加的方法
    private func addChildButtons(title:String,backImageName:String,index:Int){
    
    let btn = UIButton()
        
        btn.tag = index + baseTag
        
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: backImageName + "_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: backImageName + "selected"), forState: .Selected)
        
        //给按钮添加点击事件
        btn.addTarget(self, action: #selector(LYEmoticonToolBar.btnDidClick(_:)), forControlEvents: .TouchUpInside)
        //UIStackView添加子视图
        addArrangedSubview(btn)
        if index == 0 {
        
            btn.selected = true
            curreBtn = btn
        }
        
    }
    
    @objc private func btnDidClick(btn:UIButton){
    
        print("我被点击了")
        //先把点中按钮的状态 改变选中
        curreBtn?.selected = false
        btn.selected = true
        curreBtn = btn
        
        selectedIndexCallBack?(index:btn.tag - baseTag)
    }
    
    func setselectedBtn(section: Int){
    
        let btntag = baseTag + section
        let btn = self.viewWithTag(btntag) as! UIButton
        
        curreBtn?.selected = false
        
        btn.selected = true
        
        curreBtn = btn
        
        
    }
}
